var price = '';
var postage = '';
var buynum = '';

apiready = function() {
      var _id = api.pageParam.id;
      $api.do_get("/api/products/info?id=" + _id, function(result) {

            $("#buyNum").text(1);
            $("#gprice").text(result.price);
            $("#prototal").text(result.price);
            $("#postage").text(result.postage);
            $("#proinfoName").text(result.name);
            $("#total").text(result.price + result.postage);
            $api.getNetPicPath(result.sgImg, function(picPath) {
                  $("#info_img").attr('src', picPath);
            });

            $("#proinfoName").text(result.name);
            price = $('#gprice').text();
            postage = $('#postage').text();
            buynum = $("#buyNum").val();
      });



      // $("#orderinfo_proNames").text();
      // $("#gprice").text();

      // $("#orderInfo_proimg").attr('src', api.pageParam.product_img);
      // $("#orderinfo_product").attr('href', api.pageParam.product_link);

      // $("#prototal").text(buynum * price);
      // $("#total").text(buynum * price + parseInt(postage));

      $(document).ready(function() {
            $('.spinnerExample').spinner({
                  value: 1,
                  min: 1,
                  max: 100,
                  step: 1
            });

      });
};


$(function() {

      var _id = api.pageParam.id;
      $("#orderinfo_product").click(function() {
            $api.mOpenWin("groupInfo", "group_info.html", "{\"id\":\"{{_id}}\"}");

      });

      $("#group_join_pay").click(function() {

      })；

      $("#order_minus").click(function() {
            var buynum = $("#buyNum").val();
            if (buynum > 1) {
                  $("buyNum").val(buynum - 1);
                  $("#prototal").text((buynum - 1) * price);
                  $("#total").text((buynum - 1) * price + parseInt(postage));

            } else {

            }

      });


      $("#order_add").click(function() {
            var buynum = $("#buyNum").val();
            $("buyNum").val(buynum + 1);
            $("#prototal").text((buynum + 1) * price);
            $("#total").text((buynum + 1) * price + parseInt(postage));
      });

      $("#buyNum").change(function() {
            var price = $('#gprice').text();
            var postage = $('#postage').text();
            var buynum = this.value;
            $("#prototal").text(buynum * price);
            $("#total").text(buynum * price + parseInt(postage));

      });

      $('#btnOrder').click(function() {
            $api.getUserInf(function(user) {

                  var data = {
                        id: api.pageParam.id,
                        account: user.account,
                        address: $('.prov').val() + '-' + $('.city').val() + '-' + $('.dist').val() + '  ' + $('#address').val(),
                        buyNum: buynum.val(),
                        receiver: $('#receiver').val(),
                        phone: $('#phone').val()
                  };

                  if (data.address == '' || data.receiver == '' || data.phone == '') {
                        alert('请填写收货信息');
                        return;
                  } else {
                        var postUrl = '/api/product/order/create';
                        $api.do_post(postUrl, {
                              body: JSON.stringify({
                                    data；
                              })
                        }, function(result) {
                              if (result == '0') {
                                    alert('拼团成功');
                                    window.location.href = '/wx/pgroup/orderpay?orderno=' + response.orderno
                              } else {
                                    alert('拼团失败');
                              }
                        });
                  }



            });

      });
});